package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mall.MallActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.patrons.PatronsBreak;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.activity.eActivityChargeType;
import com.yanqu.road.entity.player.UserMallBuyCount;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.MallActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 *  新首充-现金
 */
public class NewFirstRechargeCashCondition extends BaseActivityCondition {

	public NewFirstRechargeCashCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.FirstRechargeCash.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.FirstRechargeCash.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		int mallMarkId = activityConditionInfo.getParamList().get(0).intValue();
		int checkDay = activityConditionInfo.getParamList().get(3).intValue();
		//获取礼包详情
		MallActivityInfo mallActivityInfo = MallActivityMgr.getInTimeActivity(this.activityInfo.getActivityId());
		if (mallActivityInfo == null || mallActivityInfo.getMallInfoMap() == null) {
			return false;
		}
		MallInfo targetMallInfo = null;
		for (MallInfo mallInfo : mallActivityInfo.getMallInfoMap().values()) {
			if (mallInfo.getMallMarkId() == mallMarkId) {
				targetMallInfo = mallInfo;
				break;
			}
		}
		if (targetMallInfo == null) {
			return false;
		}
		//断礼包是否购买
		UserMallBuyCount userMallBuyCount = player.getModule(ActivityMallModule.class).getUserMall(targetMallInfo.getMallId());
		if (userMallBuyCount == null) {
			return false;
		}
		if (userMallBuyCount.getActivityId() != this.activityInfo.getActivityId()) {
			return false;
		}
		if (userMallBuyCount.getCount() <= 0) {
			return false;
		}
		//判断天数是否符合
		long buyMallTime = userMallBuyCount.getLastUpdateTime();
		long todayZero = DateHelper.getTodayZeroTimeStamp();
		long rechargeDayZero = DateHelper.getZeroTimeStamp(buyMallTime * 1000);
		long day = (todayZero - rechargeDayZero) / DateHelper.DAY_MILLIONS + 1;
		//返回
		return day >= checkDay;
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {

	}

	@Override
	public String getReward() {
		String reward = super.getReward();
		try{
			//首充、镖师
			if(this.activityInfo.getType() == eActivityType.NewFirstRechargeCash.getValue()){
				Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(this.activityInfo.getActivityId());
				if(activityConfigMap != null && activityConfigMap.containsKey(eActivityConfigType.RECHARGE_REWARD_PATRONS_LEVEL.getName())){
					List<Integer> paramList = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.RECHARGE_REWARD_PATRONS_LEVEL.getName()).getValue(), "\\|");
					int patronsId = paramList.get(0);
					int level = paramList.get(1);
					Property property = PropertyHelper.parseStringToProperty(reward);
					for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
						int itemId = entry.getKey();
						//奖励有该门客
						if(GoodsMgr.isPatronsGoods(itemId) && patronsId == itemId){
							PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(itemId);
							UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsInfo.getId());
							if(userPatrons != null){
								//门客等级
								if(level > userPatrons.getLevel()) {
									userPatrons.setLevel(level);
								}
								if(paramList.size() == 3 && paramList.get(2) == 1){
									//突破等级
									int breakLv = 0;
									for (int i = 0; i < 500; i++) {
										PatronsBreak patronsBreak = PatronsMgr.getPatronsBreak(i);
										if(patronsBreak == null){
											continue;
										}
										if(level >= patronsBreak.getCurrentMaxLv()){
											breakLv = i;
										}
									}
									if(breakLv > 0){
										userPatrons.setBreakLv(breakLv);
									}
								}
								player.getModule(PatronsModule.class).syncPatronsData(userPatrons);

								player.getModule(PatronsModule.class).refreshCopyMaxPatronsLevel(false);
								break;
							}
						}
					}
				}
			}
		}catch (Exception e){
			logger.info("activity {} player {} recharge reward {} special handle error", this.activityInfo.getActivityId(), player.getUserId(), reward);
		}
		return reward;
	}
}
